/**
 * 作者: wgx
 * 日期: 2025-02-21 17:26:19
 * 备注: 玩家怪兽洞窟模块的被打记录
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserCaveRecord;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserCaveRecordModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = true;// 是否需要额外的有序列表

    protected long id; // ID
    protected long userId; // 用户ID
    protected long enemyId; // 来打我的敌人的id
    protected String userName = ""; // 对方的username
    protected int result; // 战斗结果 (0我方胜利，1-我方失败)
    protected long addTime; // 被打的时间
    protected int areaType; // 区域id
    protected int floorId; // 原来的层id
    protected int floorIdNew; // 战斗后的层id

    public void reset() {
    }

    protected String getKey() {
        return "UserCaveRecord." + id;
    }

    protected String getListKey() {
        return "UserCaveRecord." + userId;
    }

    protected String getListItemKey() {
        return id + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_cave_record"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // ID
        obj.put("userId", userId); // 用户ID
        obj.put("enemyId", enemyId); // 来打我的敌人的id
        obj.put("userName", userName); // 对方的username
        obj.put("result", result); // 战斗结果 (0我方胜利，1-我方失败)
        obj.put("addTime", addTime); // 被打的时间
        obj.put("areaType", areaType); // 区域id
        obj.put("floorId", floorId); // 原来的层id
        obj.put("floorIdNew", floorIdNew); // 战斗后的层id
        return obj;
    }

    public PBUserCaveRecord.Builder takeInitBuilder() {
        PBUserCaveRecord.Builder sb = PBUserCaveRecord.newBuilder();
        if (enemyId != 0) {
            sb.setEnemyId(enemyId);
        }
        if (!userName.isEmpty()) {
            sb.setUserName(userName);
        }
        if (result != 0) {
            sb.setResult(result);
        }
        if (addTime != 0) {
            sb.setAddTime(addTime);
        }
        if (areaType != 0) {
            sb.setAreaType(areaType);
        }
        if (floorId != 0) {
            sb.setFloorId(floorId);
        }
        if (floorIdNew != 0) {
            sb.setFloorIdNew(floorIdNew);
        }
        return sb;
    }

    public PBUserCaveRecord.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserCaveRecord.Builder sb = PBUserCaveRecord.newBuilder();
        if (isFieldUpdate("enemyId")) {
            sb.setEnemyId(enemyId);
        }
        if (isFieldUpdate("userName")) {
            sb.setUserName(userName);
        }
        if (isFieldUpdate("result")) {
            sb.setResult(result);
        }
        if (isFieldUpdate("addTime")) {
            sb.setAddTime(addTime);
        }
        if (isFieldUpdate("areaType")) {
            sb.setAreaType(areaType);
        }
        if (isFieldUpdate("floorId")) {
            sb.setFloorId(floorId);
        }
        if (isFieldUpdate("floorIdNew")) {
            sb.setFloorIdNew(floorIdNew);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public long getEnemyId() {
        return enemyId;
    }

    public void putEnemyId(long enemyId) {
        if (this.enemyId != enemyId) {
            this.enemyId = enemyId;
            updateRedisHashField(getKey(), "enemyId", this.enemyId);
        }
    }

    public void addEnemyId(long value) {
        this.enemyId += value;
        updateRedisHashField(getKey(), "enemyId", this.enemyId);
    }

    public String getUserName() {
        return userName;
    }

    public void putUserName(String userName) {
        if (!this.userName.equals(userName)) {
            this.userName = userName;
            updateRedisHashField(getKey(), "userName", this.userName);
        }
    }

    public int getResult() {
        return result;
    }

    public void putResult(int result) {
        if (this.result != result) {
            this.result = result;
            updateRedisHashField(getKey(), "result", this.result);
        }
    }

    public void addResult(int value) {
        this.result += value;
        updateRedisHashField(getKey(), "result", this.result);
    }

    public long getAddTime() {
        return addTime;
    }

    public void putAddTime(long addTime) {
        if (this.addTime != addTime) {
            this.addTime = addTime;
            updateRedisHashField(getKey(), "addTime", this.addTime);
        }
    }

    public void addAddTime(long value) {
        this.addTime += value;
        updateRedisHashField(getKey(), "addTime", this.addTime);
    }

    public int getAreaType() {
        return areaType;
    }

    public void putAreaType(int areaType) {
        if (this.areaType != areaType) {
            this.areaType = areaType;
            updateRedisHashField(getKey(), "areaType", this.areaType);
        }
    }

    public void addAreaType(int value) {
        this.areaType += value;
        updateRedisHashField(getKey(), "areaType", this.areaType);
    }

    public int getFloorId() {
        return floorId;
    }

    public void putFloorId(int floorId) {
        if (this.floorId != floorId) {
            this.floorId = floorId;
            updateRedisHashField(getKey(), "floorId", this.floorId);
        }
    }

    public void addFloorId(int value) {
        this.floorId += value;
        updateRedisHashField(getKey(), "floorId", this.floorId);
    }

    public int getFloorIdNew() {
        return floorIdNew;
    }

    public void putFloorIdNew(int floorIdNew) {
        if (this.floorIdNew != floorIdNew) {
            this.floorIdNew = floorIdNew;
            updateRedisHashField(getKey(), "floorIdNew", this.floorIdNew);
        }
    }

    public void addFloorIdNew(int value) {
        this.floorIdNew += value;
        updateRedisHashField(getKey(), "floorIdNew", this.floorIdNew);
    }

}